package com.magicsoft.share.ui.my;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.just.library.AgentWeb;
import com.just.library.ChromeClientCallbackManager;
import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.share.Article;
import com.magicsoft.share.customview.dialog.DialogShare;
import com.magicsoft.share.presenter.my.ArticleSharePresenter;
import com.magicsoft.share.ui.article.ArticleSettingActivity;
import com.magicsoft.share.ui.share.CommentListActivity;
import com.magicsoft.share.utils.DensityUtils;
import com.magicsoft.share.utils.ToastUtils;
import com.magicsoft.share.utils.WxShareUtil;
import com.magicsoft.share.view.my.IArticleShareView;

import butterknife.BindView;
import butterknife.OnClick;

public class ArticleShareActivity extends BaseActivity<IArticleShareView, ArticleSharePresenter> implements IArticleShareView {


    private DialogShare mDialogShare ;
    private String mShareUrl ;
    private String mArticalTitle ;
    private String mShareImage ;
    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_like)
    TextView tvLike;
    @BindView(R.id.tv_share)
    TextView tvShare;
    @BindView(R.id.tv_comment)
    TextView tvComment;
    @BindView(R.id.layout_bottom)
    LinearLayout layoutBottom;
    @BindView(R.id.view_bottom)
    View viewBottom;
    @BindView(R.id.relativeLayout)
    RelativeLayout relativeLayout;
    @BindView(R.id.iv_like)
    ImageView ivLike;
    private AgentWeb mAgentWeb;
    private PopupWindow mPopTop;
    private long articleId;
    private Article mArticle;
    private boolean isThumbsUp;
    private int is_allow_comment;
    @Override
    public ArticleSharePresenter initPresenter() {
        return new ArticleSharePresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_article_share;
    }

    @Override
    public void initData() {
        articleId = getIntent().getLongExtra("articleId", -1);

        initDialog();
    }

    @Override
    public void initView() {

        tvTitle.setText("分享");
        tvRight.setText("操作");

        initMyPop();

    }

    @Override
    public void requestData() {

        Log.e("SSL",articleId+"");
        mPresenter.getArticleDetail(articleId);
    }


    @OnClick({R.id.ibtn_left, R.id.tv_right, R.id.tv_like, R.id.tv_share, R.id.tv_comment, R.id.iv_like})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ibtn_left:
                finish();
                break;
            case R.id.tv_right:
                mPopTop.showAsDropDown(tvRight, 0, DensityUtils.dp2px(this, 10));
                break;
            case R.id.tv_share:
                mDialogShare.show(getSupportFragmentManager(), "");
                break;
            case R.id.tv_comment:
                //此处加入是否可以评论的判断
               if (is_allow_comment==1) {
                   Intent intent = new Intent(this, CommentListActivity.class);
                   intent.putExtra("articleId", articleId);
                   startActivity(intent);
               }else {
                    ToastUtils.showToast(this,"没有权限评论");
               }
                break;

            case R.id.iv_like:
                if (isThumbsUp) {

                    mPresenter.deleteArticleLove(mArticle.getArticleId());
                } else {
                    mPresenter.articleLove(mArticle.getArticleId());
                }
                break;
        }
    }


    private void initMyPop() {
        mPopTop = new PopupWindow(this);
        mPopTop.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        mPopTop.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        mPopTop.setFocusable(true);////获取焦点
        mPopTop.setTouchable(true);
        mPopTop.setOutsideTouchable(true);//设置popupwindow外部可点击
        //    mPopTop.update();// 刷新状态
        ColorDrawable dw = new ColorDrawable(0000000000);// 实例化一个ColorDrawable颜色为半透明
        mPopTop.setBackgroundDrawable(dw);// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        mPopTop.setAnimationStyle(R.style.QQPopAnim);//设置显示和消失动画
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View conentView = inflater.inflate(R.layout.layout_right_pop2, null);

        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.tv1:
                        startActivity(new Intent(ArticleShareActivity.this, ArticleEdtActivity.class)
                        .putExtra("Data",mArticle.getArticleId()));

                        break;
                    case R.id.tv2:
                        mPresenter.deleteArticle(articleId);
                        break;
                    case R.id.tv3:
                        Intent intent = new Intent(ArticleShareActivity.this, ArticleSettingActivity.class);
                        intent.putExtra("articleId",mArticle.getArticleId());
                        intent.putExtra("is_open",mArticle.getIs_open());
                        intent.putExtra("is_allow_comment",mArticle.getIs_allow_comment());
                        Log.d("articleId", "onClick: "+mArticle.getArticleId()+"..."+mArticle.getTitle());
                        startActivity(intent);
                        break;
                    default:
                        break;

                }
                mPopTop.dismiss();
            }
        };
        conentView.findViewById(R.id.tv1).setOnClickListener(clickListener);
        conentView.findViewById(R.id.tv2).setOnClickListener(clickListener);
        conentView.findViewById(R.id.tv3).setOnClickListener(clickListener);
        mPopTop.setContentView(conentView);

    }

    @Override
    public void getArticleDetailSuccess(Article article) {
        mArticalTitle = article.getTitle() ;
        mShareUrl = article.getArticleURL() ;
        mShareImage = article.getCover() ;
        mArticle = article;
        isThumbsUp = article.isThumbsUp();
        is_allow_comment=article.getIs_allow_comment();
        if (article.isThumbsUp()) {
            Glide.with(this).load(R.drawable.heart_bottom_red).into(ivLike);
        } else {
            Glide.with(this).load(R.drawable.heart_bottom).into(ivLike);
        }

        mAgentWeb = AgentWeb.with(this)//传入Activity or Fragment
                .setAgentWebParent(relativeLayout, new RelativeLayout.LayoutParams(-1, -1))//传入AgentWeb 的父控件 ，如果父控件为 RelativeLayout ， 那么第二参数需要传入 RelativeLayout.LayoutParams ,第一个参数和第二个参数应该对应。
                .useDefaultIndicator()// 使用默认进度条
                .defaultProgressBarColor() // 使用默认进度条颜色
                .setReceivedTitleCallback(new ChromeClientCallbackManager.ReceivedTitleCallback() {
                    @Override
                    public void onReceivedTitle(WebView view, String title) {

                    }
                }) //设置 Web 页面的 title 回调
                .createAgentWeb()
                .ready()
                .go(article.getArticleURL());

    }

    @Override
    public void articleLoveSuccess() {

        isThumbsUp = true;
        Glide.with(this).load(R.drawable.heart_bottom_red).into(ivLike);
    }

    @Override
    public void deleteArticleLoveSuccess() {
        isThumbsUp = false;
        Glide.with(this).load(R.drawable.heart_bottom).into(ivLike);
    }

    @Override
    public void deleteArticleSuccess() {
        ToastUtils.showToast(this, "删除成功");
        finish();
    }
    private void initDialog(){
        mDialogShare = DialogShare.newInstance();
        mDialogShare.setOnButtonClickListener(new DialogShare.OnButtonClickListener() {
            @Override
            public void shareToWechatFriend() {
                WxShareUtil.getInstance().init(ArticleShareActivity.this).sendToWxFriend(mShareUrl, mArticalTitle, mShareImage);

            }

            @Override
            public void shareToFriendCircle() {
                WxShareUtil.getInstance().init(ArticleShareActivity.this).sendToFriendCircle(mShareUrl, mArticalTitle, mShareImage);
            }
        });
    }
}
